/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package casino;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author amaghfur
 */
public class Player1326StateFactory {
    
    /** for singleton pattern */
    private static Player1326StateFactory instance;
    /** book keeping for states that have already been created */ 
    private Map<String,Player1326State> values = new HashMap<>();
    /** player tied to the states */
    private Player1326 player;
    
    /**
     * Class constructor
     * 
     * @param setPlayer player tied to these states
     */
    private Player1326StateFactory(Player1326 setPlayer) {
        player = setPlayer;
    }
    
    /**
     * Singleton pattern.
     * 
     * @param player    player tied to these states
     * @return          the only state factory connected to the player in question 
     */
    public static synchronized Player1326StateFactory getStateFactory(Player1326 player) {
        if (instance == null)
            instance = new Player1326StateFactory(player);
        return instance;
    }
    
    /**
     * Asks the factory to return the requested state by the state name.
     * 
     * @param stateName state name of requested state
     * @return          requested state
     */
    public Player1326State getState(String stateName) {
        if (values.containsKey(stateName) == true) // check if alread created
            return values.get(stateName); // return the existing one
        else { // create a new one
            Player1326State state;
            switch (stateName) {
                case "Player1326NoWins":
                    state = new Player1326NoWins(player);
                    values.put(stateName, state);
                    return state;
                case "Player1326OneWin":
                    state = new Player1326OneWin(player);
                    values.put(stateName, state);
                    return state;
                case "Player1326TwoWins":
                    state = new Player1326TwoWins(player);
                    values.put(stateName, state);
                    return state;
                case "Player1326ThreeWins":
                    state = new Player1326ThreeWins(player);
                    values.put(stateName, state);
                    return state;
                default :
                    return null;
            }
        }
        
    }
}
